package com.cainezhu.coolwind.utils;

import java.lang.reflect.Member;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

public class DateConverter extends DefaultTypeConverter {
	@Override
	public Object convertValue(Map<String, Object> context, Object target,
			Member member, String propertyName, Object value, @SuppressWarnings("rawtypes") Class toType) {
		// TODO Auto-generated method stub
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		if (toType == Date.class) {
			try {
				String[] date = (String[]) value;
				return dateFormat.parse(date[0]);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		} else if (toType == String.class) {
			Date date = (Date) value;

			return dateFormat.format(date);

		} else {
			return super.convertValue(context, target, member, propertyName,
					value, toType);
		}
	}
}
